package com.yf1706.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yf1706.bean.Salary;
import com.yf1706.bean.SalarySob;
import com.yf1706.service.SalaryService;
import com.yf1706.tools.JsonTool;

/**
 * 
 * @author 锦涛
 *
 */
@Controller
@RequestMapping("salaryService")
public class SalaryServiceController extends BaseController {

	@Autowired
	private SalaryService salaryService;

	// --------------薪资账套------------------------
	@RequestMapping("querySobByPage")
	@ResponseBody
	public Map<String, Object> querySobByPage(int page, int rows, String sort, String order) {
		Page<SalarySob> pages = salaryService.querySobByPage(page, rows, sort, order);
		Long total = pages.getTotalElements();
		List<SalarySob> list = pages.getContent();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	@RequestMapping("findSobByPage")
	@ResponseBody
	public Map<String, Object> findSobByPage(String salarySobName, int page, int rows, String sort, String order) {
		Page<SalarySob> pages = salaryService.findSearchSob(salarySobName, page, rows, sort, order);
		Long total = pages.getTotalElements();
		List<SalarySob> list = pages.getContent();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	@RequestMapping("insertSob")
	@ResponseBody
	public void insertSob(HttpServletRequest request, HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_Ssob_rowData"));
		SalarySob salarySob = new SalarySob();
		salarySob.setSalarySobName(jObject.getString("salarySobName"));
		salarySob.setRemark(jObject.getString("remark"));
		salarySob.setBuilder(getSessionUserName(request));
		salarySob.setBuildDate(new Date());
		JsonTool jTool = new JsonTool();
		try {
			salaryService.saveSob(salarySob);
			jTool.setSuccess(true);
			jTool.setMsg("添加薪资账套【" + salarySob.getSalarySobName() + "】成功！");
		} catch (Exception e) {
			jTool.setMsg("添加薪资账套【" + salarySob.getSalarySobName() + "】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("updateSob")
	@ResponseBody
	public void updateSob(HttpServletRequest request, HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_Ssob_rowData"));
		SalarySob salarySob = new SalarySob();
		salarySob.setSalarySobId(jObject.getIntValue("salarySobId"));
		salarySob.setSalarySobName(jObject.getString("salarySobName"));
		salarySob.setRemark(jObject.getString("remark"));
		
		JsonTool jTool = new JsonTool();
		try {
			salaryService.updateSob(salarySob);
			jTool.setSuccess(true);
			jTool.setMsg("编辑薪资账套【" + salarySob.getSalarySobName() + "】成功！");
		} catch (Exception e) {
			jTool.setMsg("编辑薪资账套【" + salarySob.getSalarySobName() + "】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("deleteSob")
	@ResponseBody
	public void deleteSob(HttpServletRequest request, HttpServletResponse response) {
		String ids = removeFirstAndLastChar(request.getParameter("djt_Ssob_rowData"));
		JsonTool jTool = new JsonTool();
		try {
			salaryService.removeSob(ids);
			jTool.setSuccess(true);
			jTool.setMsg("删除薪资账套【成功】！");
		} catch (Exception e) {
			jTool.setMsg("删除薪资账套【失败】！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("queryAllSob")
	@ResponseBody
	public List<SalarySob> queryAllSob() {
		return salaryService.querySobAll();
	}

	// --------------薪资表------------------------
	@RequestMapping("findSByPage")
	@ResponseBody
	public Map<String, Object> findSbypage(int id, int page, int rows, String sort, String order) {
		Page<Salary> pages = salaryService.querySByPage(id, page, rows, sort, order);
		Long total = pages.getTotalElements();
		List<Salary> list = pages.getContent();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	@RequestMapping("insertS")
	@ResponseBody
	public void insertS(HttpServletRequest request, HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_S_rowData"));
		Salary salary = new Salary();
		salary.setSalaryName(jObject.getString("salaryName"));
		String a= request.getParameter("salarySobId");
		int salarySobId = Integer.parseInt(a);
		salary.setSalarySob(salaryService.findSobByID(salarySobId));
		salary.setSalaryBeginDate(jObject.getDate("salaryBeginDate"));
		salary.setSalaryOffDate(jObject.getDate("salaryOffDate"));
		// 运算状态：1-已运算、0-未运算
		salary.setCountStatus((byte) 0);
		salary.setRemark(jObject.getString("remark"));
		// 改为修改者，修改日期
		salary.setApprover(jObject.getString(getSessionUserName(request)));
		salary.setApproveDate(new Date());
		// salary.setApproveStatus(jObject.getByteValue("approveStatus"));
		// salary.setSalaryTotal(jObject.getIntValue("salaryTotal"));
		JsonTool jTool = new JsonTool();
		try {
			salaryService.saveS(salary);
			jTool.setSuccess(true);
			jTool.setMsg("添加薪资表：【" + salary.getSalaryName() + "】成功！");
		} catch (Exception e) {
			jTool.setMsg("添加薪资表：【" + salary.getSalaryName() + "】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("updateS")
	@ResponseBody
	public void updateBS(HttpServletRequest request, HttpServletResponse response) {
		JSONObject jObject = JSON.parseObject(request.getParameter("djt_S_rowData"));
		Salary salary = new Salary();
		salary.setSalaryId(jObject.getIntValue("salaryId"));
		salary.setSalaryName(jObject.getString("salaryName"));
		salary.setSalaryBeginDate(jObject.getDate("salaryBeginDate"));
		salary.setSalaryOffDate(jObject.getDate("salaryOffDate"));
		// 改为修改者，修改日期
		salary.setApprover(jObject.getString(getSessionUserName(request)));
		salary.setApproveDate(new Date());
		salary.setRemark(jObject.getString("remark"));
		JsonTool jTool = new JsonTool();
		try {
			salaryService.updateS(salary);
			jTool.setSuccess(true);
			jTool.setMsg("编辑薪资表：【" + salary.getSalaryName() + "】成功！");
		} catch (Exception e) {
			jTool.setMsg("编辑薪资表：【" + salary.getSalaryName() + "】失败！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("deleteS")
	@ResponseBody
	public void deleteS(HttpServletRequest request, HttpServletResponse response) {
		String ids = removeFirstAndLastChar(request.getParameter("djt_S_rowData"));
		JsonTool jTool = new JsonTool();
		try {
			salaryService.removeS(ids);
			jTool.setSuccess(true);
			jTool.setMsg("删除薪资表【成功】！");
		} catch (Exception e) {
			jTool.setMsg("删除薪资表【失败】！");
			e.printStackTrace();
		}
		writeJson(jTool, response);
	}

	@RequestMapping("querySAll")
	@ResponseBody
	public List<Salary> querySAll() {
		return salaryService.querySAll();
	}

}
